package al.xc.wxsession.config;

import al.xc.thirdparty.wechat.authorization.IWechatConfig;
import al.xc.thirdparty.wechat.authorization.SingleConfig;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;
import java.util.List;

@Configuration
@ConfigurationProperties("wechat")
public class WechatConfigImpl implements IWechatConfig {

    /**
     * 用户token获取url
     */
    private String userTokenUrl;

    /**
     * 全局token获取url
     */
    private String globalTokenUrl;
    /**
     * 发送订阅消息url
     */
    private String sendSubscribeUrl;

    /**
     * 帐号信息
     */
    private List<SingleConfig> info;

    /**
     * 设置用户token获取url
     * @return
     */
    public void setUserTokenUrl(String userTokenUrl) {
        this.userTokenUrl = userTokenUrl;
    }

    /**
     * 设置局token获取url
     * @return
     */
    public void setGlobalTokenUrl(String globalTokenUrl) {
        this.globalTokenUrl = globalTokenUrl;
    }

    /**
     * 设置订阅消息url
     * @param sendSubscribeUrl
     */
    public void setSendSubscribeUrl(String sendSubscribeUrl) { this.sendSubscribeUrl = sendSubscribeUrl; }

    /**
     * 获取用户token的url
     * @return
     */
    public String getUserTokenUrl() {
        return userTokenUrl;
    }

    /**
     * 获取全局token的url
     *
     * @return
     */
    @Override
    public String getGlobalTokenUrl() {
        return globalTokenUrl;
    }

    /**
     * 发送订阅消息url
     *
     * @return
     */
    @Override
    public String getSendSubscribeUrl() {
        return sendSubscribeUrl;
    }

    /**
     * 设置帐号信息
     * @param info
     */
    public void setInfo(List<SingleConfig> info) {
        this.info = info;
    }

    /**
     * 获取对应appid的信息
     * @param appId
     * @return
     */
    public SingleConfig get(String appId) {
        if (null == info) {
            return null;
        }
        for (SingleConfig config : info) {
            if (config.getAppid().equals(appId)) {
                return config;
            }
        }
        return null;
    }

    /**
     * 列出所有配置
     *
     * @return
     */
    @Override
    public List<SingleConfig> listConfig() {
        return new ArrayList<>(info);
    }
}
